package com.rainy.upload.action;

import com.rainy.upload.service.ImgUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/img")
public class ImgUploadController {

    @Autowired
    private ImgUploadService imgUploadService;

    @ResponseBody
    @RequestMapping("/uploadFile/{userName}")
    public Object uploadFile(@PathVariable String userName, HttpServletRequest request) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile uploadFile = multipartRequest.getFile("file");
        String filePath = imgUploadService.uploadFile(userName, uploadFile);
        return filePath;
    }

}
